// pages/homePage/homePage.js
var app = getApp();

Page({
	/**
	 * 页面的初始数据
	 */
	data: {

		indicatorDots: true,
		autoplay: true,
		interval: 5000,
		duration: 1000,
		productType: [],
		videoURL: '',
		choo_modal: 'none'
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.dialog = this.selectComponent("#dialog");
		this.dialog.showDialog();
		// this.getUserLocation();
		wx.showTabBar({})
		var that = this;
		//获取手机高度
		that.setData({
			navH: app.globalData.navHeight,
			partnerUserId: options.partnerUserId,
			giving: options.giving, //是否赠送
			usergiftcardid: options.usergiftcardid,
			cardid: options.cardid,
			partnerUserName: options.partnerUserName,
			price: options.price
		})
		if (options.parentUserId == undefined) {
			var parentUserId = wx.getStorageSync("parentUserId");
			that.setData({
				parentUserId: parentUserId,
			})
			if (parentUserId != undefined) {
				wx.request({
					url: app.globalData.url + "getUserByUserId",
					method: "GET",
					data: {
						userId: wx.getStorageSync("parentUserId"),
					},
					success: function(res) {
						if (res.data.result == 1) {
							that.setData({
								parentName: res.data.data.nickName
							})
						}
					}
				});
			}

		}

		if (options.partnerUserId != undefined) {
			wx.setStorageSync("partnerUserId_nlc", options.partnerUserId)
		}


		if (options.cardid == undefined) {
			var cardid = wx.getStorageSync("cardid");
			that.setData({
				cardid: cardid,
			})
		}
		if (options.price == undefined) {
			var price = wx.getStorageSync("price");
			that.setData({
				price: price,
			})
		}
		if (options.partnerUserName == undefined) {
			var partnerUserName = wx.getStorageSync("partnerUserName");
			that.setData({
				partnerUserName: partnerUserName,
			})
		}
		if (options.giving == undefined) {
			var giving = wx.getStorageSync("giving");
			that.setData({
				giving: giving
			})
		}
		if (options.usergiftcardid == undefined) {
			var usergiftcardid = wx.getStorageSync("usergiftcardid");
			that.setData({
				usergiftcardid: usergiftcardid
			})
		}


		wx.loadFontFace({
			family: 'fonts',
			source: 'url("https://test.jwsxmall.com/jiweishengxian/VisbyCF-Bold.otf")',
			success(res) {
				console.log(res.status)
				that.setData({
					loaded: true
				})
			},
			fail: function(res) {
				console.log(res.status)
			},
			complete: function(res) {
				console.log(res.status)
			}
		});


		//弹窗
		console.info('zhehsi giving' + that.data.giving);
		console.info('zhehsi cardid' + that.data.usergiftcardid);

		if (that.data.usergiftcardid != '' && that.data.giving == 1) {
			console.log("进入礼品卡了")

			wx.request({
				url: app.globalData.url + "getUserReceiveCardId",
				method: "GET",
				data: {
					cardId: that.data.usergiftcardid
				},
				success: function(res) {
					console.log(res.data.data)
					var data = res.data.data;
					if (res.data.result == 1) {
						that.setData({
							price: res.data.data.price
						})
						console.log("是否领取：" + data.receive)
						if (data.receive == true) {
							if (that.data.partnerUserId != wx.getStorageSync("userId")) {
								that.showModal();
							}
						}
					}
				}
			})
		} else if (that.data.cardid != '' && that.data.giving == 2) {
			wx.request({
				url: app.globalData.url + "getUserStoredValueCard",
				method: "GET",
				data: {
					cardId: that.data.cardid,
				},
				success: function(res) {
					var data = res.data.data;
					if (res.data.result == 1) {
						if (data.receive == true) {
							if (that.data.partnerUserId != wx.getStorageSync("userId")) {
								that.showModal2();
							}
						}
					}
				}
			})
		} else {
			console.info("这是优惠券的弹窗");
			wx.request({
				url: app.globalData.url + "getPopupCouponList",
				method: "GET",
				data: {
					userId: wx.getStorageSync("userId"),
				},
				success: function(res) {
					var data = res.data.data;
					if (res.data.result == 1 && res.data.data != '') {
						that.setData({
							popupCoupon: res.data.data[0]
						})
						that.showModal1();
					} else {

					}
				}
			})
		};
		//分享banner
		wx.request({
			url: app.globalData.url + "getAdvertisementListByType?advertisingType=8",
			method: "GET",
			data: {},
			success: function(res) {
				var data = res.data.data;
				if (res.data.result == 1) {
					var link = [];
					for (var i = 0; i < data.length; i++) {
						var link0 = data[i].link;
						var a = link0.substr(link0.length - 1);
						data[i].linkType = a;
					}
					that.setData({
						shareImg: data,
					})
				}
			}
		})
	},
	/**
	 * 页面分享
	 */
	onShareAppMessage: function() {
		var that = this;
		console.log("userId:-----------------------" + wx.getStorageSync("userId"))
		return {
			title: "极味生鲜",
			desc: '分享页面的内容',
			// imageUrl: that.data.shareImg[0].imageURL,
			imageUrl: "http://file.jwsxmall.com/shareImg.jpg",
			path: '/pages/mallPage/mallPage?partnerUserId=' + wx.getStorageSync("userId") +
				'&type=2', // 路径，传递参数到指定页面
		}
	},
	/**
	 * 页面朋友圈
	 */
	onShareTimeline: function(res) {
			var that = this;
		return {
			title: "极味生鲜",
			desc: '分享页面的内容',
			// imageUrl: that.data.shareImg[0].imageURL,
			imageUrl: "http://file.jwsxmall.com/shareImg.jpg",
			path: '/pages/mallPage/mallPage?partnerUserId=' + wx.getStorageSync("userId") +
				'&type=2', // 路径，传递参数到指定页面
		}

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		var that = this;

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		var that = this;
		that.setData({
			token: wx.getStorageSync("token")
		});
		wx.request({
			url: app.globalData.url + "getUserByWechatUnoin",
			method: "GET",
			data: {
				wechatUnoinId: wx.getStorageSync("wechatUnoinId"),
				token: that.data.token
			},
			success: function(res) {
				var data = res.data;
				if (data.result == 1) {

					if (data.data.notificationToRead > 0) {
						that.setData({
							notificationToRead: data.data.notificationToRead,
							inforImg: "http://file.jwsxmall.com/information1.png"
						})
					} else {
						that.setData({
							notificationToRead: data.data.notificationToRead,
							inforImg: "http://file.jwsxmall.com/information%20.png"
						})
					}
				} else {

				}
			}
		});
		if (that.data.partnerUserId != undefined && that.data.partnerUserId != '') {
			wx.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {} else {
						// 未授权，跳转到授权页面
						wx.reLaunch({
							url: '/pages/zcuslist/index?partnerUserId=' + that.data
								.partnerUserId + '&giving=' + that.data.giving +
								'&usergiftcardid=' + that.data.usergiftcardid +
								'&price=' + that.data.price + '&cardid=' + that.data
								.cardid + '&partnerUserName=' + that.data
								.partnerUserName,
						})
					}
				}
			})
		}
		/**
		 * 购物车数量
		 */
		wx.request({
			url: app.globalData.url + "countShoppingCartNum",
			method: "GET",
			data: {
				userId: wx.getStorageSync("userId"),
			},
			success: function(res) {
				var data = res.data.data;
				that.setData({
					cartCount: data
				})
			}
		});
		//商城首页banner
		wx.request({
				url: app.globalData.url + "getAdvertisementListByType?advertisingType=1",
				method: "GET",
				data: {},
				success: function(res) {
					var data = res.data.data;
					if (res.data.result == 1) {
						var link = [];
						for (var i = 0; i < data.length; i++) {
							var link0 = data[i].link;
							var a = link0.substr(link0.length - 1);
							data[i].linkType = a;
						}
						that.setData({
							mallBanner: data,
						})
					}
				}
			}),
			// //商城首页下面
			// wx.request({
			//   url: app.globalData.url + "getAdvertisementListByType?advertisingType=2",
			//   method: "GET",
			//   data: {
			//   },
			//   success: function (res) {
			//     var data = res.data.data;
			//     if (res.data.result == 1) {
			//       that.setData({
			//         lowImagg: data,
			//       })
			//     }
			//   }
			// }),
			//商城首页优惠券banner
			wx.request({
				url: app.globalData.url + "getAdvertisementListByType?advertisingType=11",
				method: "GET",
				data: {},
				success: function(res) {
					var data = res.data.data;
					if (res.data.result == 1 && res.data.data.length > 0) {
						that.setData({
							couponBannerImageURL: data[0].imageURL,
							couponBannerAdvertisingId: data[0].advertisingId,
						})
					}
				}
			}),
			/**
			 * 产品推荐
			 */
			wx.request({
				url: app.globalData.url + "getProductRecommendList",
				method: "GET",
				data: {
					userId: wx.getStorageSync("userId")
				},
				success: function(res) {
					var data = res.data.data;
					if (res.data.result == 1) {
						for (var i = 0; i < res.data.data.length; i++) {
							if (res.data.data[i].promotionMsg != undefined) {
								var arr = res.data.data[i].promotionMsg.split(",");
								res.data.data[i].arr = arr;
							} else {
								res.data.data[i].arr = [];
							}
							if (res.data.data[i].promotionType != undefined) {
								//先按照逗号分割字符串
								let arrycode = res.data.data[i].promotionType.split(",");
								//去重
								arrycode = Array.from(new Set(arrycode));
								//拼接正确的nextOperatorCode
								res.data.data[i].promotionType = arrycode = arrycode
									.toLocaleString();
								// var promotionType = res.data.data[i].promotionType.split(",");
								// res.data.data[i].promotionType = promotionType;
							} else {
								res.data.data[i].promotionType = [];
							}
						}

						that.setData({
							productRecommend: data
						})
					}
				}
			});

		/**
		 * 获取4个类
		 */
		wx.request({
			url: app.globalData.url + "getCategoryTagsMall",
			method: "GET",
			data: {},
			success: function(res) {
				console.log(res.data.data)
				that.setData({
					jizhiid: res.data.data[0].tagId,
					jizhiimg: res.data.data[0].image,
					weimeiId: res.data.data[1].tagId,
					weimeiimg: res.data.data[1].image,
					shengtaiId: res.data.data[2].tagId,
					shengtaiimg: res.data.data[2].image,
					xianhuoId: res.data.data[3].tagId,
					xianhuoimg: res.data.data[3].image,
				})
			}
		});
		wx.request({
			url: app.globalData.url + "getAdvertisementListByType",
			header: {
				"content-type": "application/x-www-form-urlencoded"
			},
			data: {
				advertisingType: 2,
			},
			method: "POST",
			success: function(res) {
				var data = res.data;
				if (data.result === 1) {

					that.setData({
						advertisement: data.data
					})
					data.data.forEach(element => {
						if (element && element.imageURL.indexOf('.mp4') != -1) {
							that.setData({
								videoURL: element.imageURL
							})
						}
					});
				}
			}
		});
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},


	/**
	 * 跳转到产品详情
	 */
	details: function(res) {
		var productid = res.currentTarget.dataset.productid;
		var type = res.currentTarget.dataset.type;
		wx.navigateTo({
			url: '/pages/productDetail/productDetail?productid=' + productid + '&type=' + type,
		})

	},
	/**
	 * 跳转到产品分类
	 */
	goClass: function(res) {
		var type = res.currentTarget.dataset.type;
		var typeid = res.currentTarget.dataset.typeid;
		wx.navigateTo({
			url: '/pages/productParentType/productParentType?productType=' + type + '&typeid=' +
				typeid,
		})

	},
	cart: function(e) {
		var that = this;
		var userId = wx.getStorageSync("userId");
		var productid = e.currentTarget.dataset.productid
		wx.request({
			url: app.globalData.url + "modifyShoppingCart",
			method: "GET",
			data: {
				userId: userId,
				num: 1,
				productId: productid
			},
			success: function(res) {
				var data = res.data.data;
				if (res.data.result == 1) {
					that.setData({
						productType: data
					})
				}
			}
		});
	},
	classification: function(res) {
		var that = this;
		var typeid = res.currentTarget.dataset.typeid;
		console.info(typeid);
		wx.navigateTo({
			url: '/pages/productListNew/productListNew?typeId=' + typeid,
		})
	},
	more: function() {
		var that = this;
		wx.navigateTo({
			url: '/pages/productCategories/productCategories',
		})
	},
	discount: function() {
		wx.navigateTo({
			url: '/pages/discount/discount',
		})
	},
	seckill: function() {
		wx.navigateTo({
			url: '/pages/seckill/seckill',
		})
	},
	chou: function() {
		wx.navigateTo({
			url: '/pages/chou/chou',
		})
	},
	// 立即购买\加入购物车
	immeBuy(e) {
		var that = this;
		wx.request({
			url: app.globalData.url + "modifyShoppingCart", //给函数传递服务器地址参数 
			data: {
				productId: e.currentTarget.dataset.productid,
				userId: wx.getStorageSync("userId"),
				num: 1,
			}, //给服务器传递数据，本次请求不需要数据，可以不填
			header: {
				'content-type': 'application/json' // 默认值，返回的数据设置为json数组格式
			},
			success: function() { //打印出返回的数据
				//that.toCar();
				// that.toCar();
				wx.showToast({
					title: '加入购物车成功',
					icon: 'success',
					duration: 2000
				});
				/**
				 * 购物车数量
				 */
				wx.request({
					url: app.globalData.url + "countShoppingCartNum",
					method: "GET",
					data: {
						userId: wx.getStorageSync("userId"),
					},
					success: function(res) {
						var data = res.data.data;
						that.setData({
							cartCount: data
						})
					}
				});
			}
		})
	},
	preferential: function() {
		wx.navigateTo({
			url: '/pages/couponCenter/couponCenter',
		})
	},
	tofabu: (event) => {
		wx.navigateTo({
			url: '/pages/shoppingCart/shoppingCart',
		})
	},
	//扫码
	getScancode: function() {
		var that = this;
		wx.scanCode({
			success: (res) => {
				var result = res.result;
				console.info(res);
				if (res.result.indexOf("https") >= 0) {
					var result1 = result.replace('?', '&');
					wx.navigateTo({
						url: '/pages/webView/webView?url=' + result1,
					})
				} else {
					var str = res.result.split("#");
					wx.request({
						url: app.globalData.url + "scanQRCode",
						method: "GET",
						data: {
							qrId: str[1],
						},
						success: function(res) {
							if (res.data.result == 1) {
								wx.navigateTo({
									url: '/pages/sweepCodeResults/sweepCodeResults?str=' +
										str,
								})
							} else {
								wx.showToast({
									title: '扫描失败',
									icon: 'success',
								});
							}
						}
					});


				}

			}
		})
	},
	goFind: function() {
		wx.navigateTo({
			url: '/pages/search/search',
		})
	},
	showModal() {
		this.setData({
			modalName: 'Image',
			choo_modal: 'block'
		})
	},
	showModal2() {
		this.setData({
			modalName2: 'Image',
			choo_modal: 'block'
		})
	},
	hideModal2(e) {
		this.setData({
			modalName2: null,
			choo_modal: 'none'
		})
	},
	hideModal(e) {
		this.setData({
			modalName: null,
			choo_modal: 'none'
		})
	},
	showModal1() {
		this.setData({
			modalName1: 'Image',
			choo_modal: 'block'
		})
	},
	hideModal1(e) {
		this.setData({
			modalName1: null,
			choo_modal: 'none'
		})
	},
	//领取礼品卡
	receive: function() {
		var that = this;
		wx.request({
			url: app.globalData.url + "userReceiveGiftCard",
			method: "GET",
			data: {
				userId: wx.getStorageSync("userId"),
				userGiftCardId: that.data.usergiftcardid
			},
			success: function(res) {
				var data = res.data.data;
				if (res.data.result) {
					wx.showToast({
						title: '领取成功',
						icon: 'success',
						duration: 2000
					});
				}
				wx.setStorageSync("partnerUserId", '');
				wx.setStorageSync("giving", '');
				wx.setStorageSync("usergiftcardid", '');
			}
		});
		that.hideModal();
	},
	//领取储值卡
	receiveStored: function() {
		var that = this;
		wx.request({
			url: app.globalData.url + "userReceiveSharedCard",
			method: "GET",
			data: {
				cardId: that.data.cardid,
				userId: wx.getStorageSync("userId")
			},
			success: function(res) {
				var data = res.data.data;
				if (res.data.result) {
					wx.showToast({
						title: '领取成功',
						icon: 'success',
						duration: 2000
					});
				}
				wx.setStorageSync("partnerUserId", '');
				wx.setStorageSync("giving", '');
				wx.setStorageSync("usergiftcardid", '');
				wx.setStorageSync("cardId", '');
			}
		});
		that.hideModal2();
	},

	mentionCarPresent: function() {
		wx.navigateTo({
			url: '/pages/mentionCarPresent/mentionCarPresent',
		})
	},
	moreAndMore: function() {
		wx.navigateTo({
			url: '/pages/couponCenter/couponCenter',
		})
	},
	toIntegralMall: function() {
		wx.navigateTo({
			url: '/pages/integralMall/integralMall',
		})

	},
	checkIn: function() {
		wx.navigateTo({
			url: '/pages/checkIn/checkIn',
		})
	},
	goSeckill: function() {
		wx.navigateTo({
			url: '/pages/seckill/seckill',
		})
	},
	goGroup: function() {
		wx.navigateTo({
			url: '/pages/communityGroup/communityGroup',
		})
	},
	mentionCargo: function() {
		wx.navigateTo({
			url: '/pages/mentionCargo/mentionCargo'
		})
	},
	invitation: function() {
		wx.navigateTo({
			url: '/pages/invitation/invitation',
		})
	},
	toMyPage: function() {
		wx.reLaunch({
			url: '/pages/myPage/myPage',
		})
	},
	tolive: function() {
		wx.reLaunch({
			url: '/pages/freshLife/freshLife?type=2',
		})
	},
	userReceiveCoupon: function() {
		var that = this;
		wx.getSetting({
			success: res => {
				if (res.authSetting['scope.userInfo']) {
					wx.request({
						url: app.globalData.url + "userReceiveCoupon",
						method: "GET",
						data: {
							userId: wx.getStorageSync("userId"),
							couponId: that.data.popupCoupon.couponId
						},
						success: function(res) {
							var data = res.data.data;
							if (res.data.result == 1) {
								wx.showToast({
									title: '领取成功',
									icon: 'success',
									duration: 2000
								});
								that.hideModal1();
							}
						}
					});
				} else {
					wx.reLaunch({
						url: '/pages/zcuslist/index?partnerUserId=' + that.data
							.partnerUserId + '&coupon=1',
					})
				}
			}
		})
	},
	imgbutton: function(e) {
		if (e.currentTarget.dataset.type == 1) {
			wx.navigateTo({
				url: '/pages/webView/webView?url=' + e.currentTarget.dataset.linik
			});
		} else if (e.currentTarget.dataset.type == 2) {
			if (linik == '/pages/activityPage/activityPage') {
				wx.switchTab({
					url: '' + e.currentTarget.dataset.linik,
				})
			} else {
				wx.navigateTo({
					url: '' + e.currentTarget.dataset.linik,
				})
			}

		} else if (e.currentTarget.dataset.type == 3) {
			console.info(e.currentTarget.dataset.linktype);
			if (e.currentTarget.dataset.linktype == 4) { //积分商品
				wx.navigateTo({
					url: '/pages/integralDetail/integralDetail?productId=' + e.currentTarget.dataset
						.productid + '&type=1',
				})
			}
			if (e.currentTarget.dataset.linktype == 1) { //秒杀商品
				wx.navigateTo({
					url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset
						.productid + '&type=1',
				})
			}
			if (e.currentTarget.dataset.linktype == 2) { //拼团商品
				wx.navigateTo({
					url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset
						.productid + '&type=2',
				})
			}
			if (e.currentTarget.dataset.linktype == 6) { //礼品卡商品

				wx.navigateTo({
					url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset
						.productid + '&lipinka=3&type=6'
				})
			}
			if (e.currentTarget.dataset.linktype == 5) { //预售商品
				wx.navigateTo({
					url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset
						.productid + '&type=5&yushou=1',
				})
			}
			if (e.currentTarget.dataset.linktype == 0) { //普通商品
				wx.navigateTo({
					url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset
						.productid + '&type=1',
				})
			}



		}
	},
	imgGoDetail: function(e) {
		console.log(e.currentTarget.dataset.link)
		wx.navigateTo({
			url: '' + this.data.advertisement[4].link,
		})
	},
	goInformation: function(e) {
		wx.navigateTo({
			url: '/pages/information/information',
		})
	},
	goPresell: function() {
		wx.navigateTo({
			url: '/pages/presell/presell',
		})
	},
})